package main

import (
	"fmt"
	"go-guide/network/udpv3"
	"net"
)

func main() {
	udpAddr, err := net.ResolveUDPAddr("udp", udpv3.Address)
	if err != nil {
		panic(err)
	}
	udpConn, err := net.ListenUDP("udp", udpAddr)
	if err != nil {
		panic(err)
	}
	defer udpConn.Close()

	recvChunk := make([]byte, 10*1024)
	sendChunk := "Hello Client"
	for {

		recvLen, clientAddr, err := udpConn.ReadFromUDP(recvChunk)

		if err != nil {
			panic(err)
		}

		fmt.Printf("recv from client: %s and message length is: %d\n",
			clientAddr,
			recvLen)

		sendLen, err := udpConn.WriteToUDP([]byte(sendChunk), clientAddr)
		if err != nil {
			panic(err)
		}

		fmt.Printf("send msg length: %d", sendLen)
	}
}
